define(['angular',"text!www/shop/shop_list.html",'url','common'], function (angular,tpl) {
    //angular会自动根据controller函数的参数名，导入相应的服务
   function controller($scope,$rootScope,$http){
	   	showSpin();
		//mapMenu();
	   	bottomMenu();
		$rootScope.config.map = "map";
		 var aFunction = function () {
			   this.longitude =0;
		       this.latitude =0;
		       this.speed = 0;
		       this.accuracy = 0;
		       this.items = [];
		       this.busy = false;
		       this.page = 1;
		       this.adUrl = ip+"/"+"data/afficheimg/";
		 };
		 $scope.shop =  new aFunction();
		 function getDistance(items){
			   for( var o in items){
				   if(this.latitude==0&&this.longitude==0){
					   items[o].distance =parseInt(0);
					   items[o].distanceShow ="";
				   }else{
					   items[o].distance =parseInt(0);
					   items[o].distanceShow ="";
//					   this.items[o].distance = parseInt(getFlatternDistance(this.items[o].latitude,this.items[o].longitude,this.latitude,this.longitude));//lat1,lng1,lat2,lng2
//					   this.items[o].distanceShow = this.items[o].distanceShow+"m";
				   }
			   }
			   return items;
		   };
		   $scope.shop.nextPage = function () {
			   if(this.page!=1){
				   if(!isFooter()) return ;
			   }
		       if (this.busy) return;
		       this.busy = true;
		       var pro = this;
		       var url = cl.getMapListPageUrl+"&callback=JSON_CALLBACK&page="+ this.page ;
		       $http.jsonp(url).success(function (res) {
		      	//处理成功的情况
		    	   if(res.code=="CL02021"){
		    		   if(res.result.length!=0){
				             var items = res.result;
				             items = getDistance(items);
				             for (var i = 0; i < items.length; i++) {
				            	 pro.items.push(items[i]);
				             }
				             pro.page += 1;
						}
			      	}else if(res.code=="CL02022"){
			      		
			      	}
		    	   if(pro.items.length==0){
		    		   $rootScope.changeNull();
		    	   }else{
			      		$rootScope.hideNull();
			       }
		    	   pro.busy = false;
		    	   hideSpin();
		       });
		   };
		   $scope.shop.toAddress = function(add){
						var url = "http://api.map.baidu.com/marker?location=";
						var location = add.latitude+","+add.longitude;
					//	var location = '23.477631,113.173263';
						var content='地址：'+add.address;
						var title = add.storeName;
						window.location.href = url+location+"&title="+title+"&content="+content+"&output=html&src=chunle";
			}
		   
		 $scope.shop.longitude = Number(cl.user.longitude);
		 $scope.shop.latitude = Number(cl.user.latitude);
		 $scope.shop.nextPage();
		 $scope.distance = "distance";
    };
    return {controller:controller,tpl:tpl};
});
